package com.luguz.controller;

import com.alibaba.fastjson.JSON;
import com.luguz.common.HotSearch.HotSearchDao;
import com.luguz.common.Result;
import com.luguz.service.HotSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author Guz
 * @create 2022-03--02 13:22
 */
@RestController
public class HotSearchController {
    @Autowired
    HotSearchService hotSearchService;

    @GetMapping("/api/hotSearch")
    public Result getHotSearch(){
        Map<String, ArrayList<HotSearchDao>> hotSearch = hotSearchService.getHotSearch();
        if(JSON.toJSONString(hotSearch) == "{}" || hotSearch ==null) hotSearchService.storageHotSearch();
        return Result.succ(hotSearch);
    }

    @PostConstruct
//    Java中该注解的说明：@PostConstruct该注解被用来修饰一个非静态的void（）方法。
//    被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行，并且只会被服务器执行一次。
//    PostConstruct在构造函数之后执行，init（）方法之前执行
//    spring执行顺序 Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)
    @Scheduled(cron = "0 0/5 * * * ? ") //定时器每五分钟执行一次
    public boolean storageHotSearch(){
        return hotSearchService.storageHotSearch();
    }
}
